from PyQt5.QtGui import QFont, QRegExpValidator, QIcon
from PyQt5.QtCore import pyqtSignal, Qt, QRegExp
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QApplication, \
    QLabel, QLineEdit, QPushButton, QMessageBox, QDialog, QFormLayout, QVBoxLayout
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from qdarkstyle import load_stylesheet_pyqt5

class Register_Widget(QDialog):
    Register_success_signal = pyqtSignal(str)
    def __init__(self):
        super().__init__()
        self.resize(255, 255)
        self.setWindowTitle('天空偏振光分布模式仿真与探测系统')
        self.setup_ui()

    def setup_ui(self):
        self.BiaoTi_Label = QLabel('注  册')
        font = QFont()
        font.setPixelSize(50)
        self.BiaoTi_Label.setFont(font)

        self.BiaoTi_Label.setAlignment(Qt.AlignCenter)
        self.BiaoTi_Label.setFixedHeight(100)

        self.ZhangHao_Label = QLabel('帐    号：')
        self.XingMing_Label = QLabel('姓    名：')
        self.MiMa_Label = QLabel('密    码：')
        self.QueRenMiMa_Label = QLabel('确认密码：')

        font.setPixelSize(16)
        self.ZhangHao_Label.setFont(font)
        self.XingMing_Label.setFont(font)
        self.MiMa_Label.setFont(font)
        self.QueRenMiMa_Label.setFont(font)

        self.ZhangHao_LineEdit = QLineEdit()
        self.XingMing_LineEdit = QLineEdit()
        self.MiMa_LineEdit = QLineEdit()
        self.ZhangHao_LineEdit.setPlaceholderText("")
        self.XingMing_LineEdit.setPlaceholderText("英文/汉字")
        self.MiMa_LineEdit.setPlaceholderText("大/小英文+数字")
        self.QueRenMiMa_LineEdit = QLineEdit()

        self.ZhangHao_LineEdit.setFont(font)
        self.XingMing_LineEdit.setFont(font)
        self.MiMa_LineEdit.setFont(font)
        self.QueRenMiMa_LineEdit.setFont(font)

        self.ZhangHao_LineEdit.setMaxLength(10)
        self.XingMing_LineEdit.setMaxLength(5)
        self.MiMa_LineEdit.setMaxLength(16)
        self.QueRenMiMa_LineEdit.setMaxLength(16)

        self.MiMa_LineEdit.setEchoMode(QLineEdit.Password)
        self.QueRenMiMa_LineEdit.setEchoMode(QLineEdit.Password)

        self.ZhuCe_Button = QPushButton('注 册')
        font.setPixelSize(16)
        self.ZhuCe_Button.setFont(font)
        self.ZhuCe_Button.setFixedHeight(32)
        self.ZhuCe_Button.setFixedWidth(140)

        self.FormLayout = QFormLayout()
        self.FormLayout.addRow(self.ZhangHao_Label, self.ZhangHao_LineEdit)
        self.FormLayout.addRow(self.XingMing_Label, self.XingMing_LineEdit)
        self.FormLayout.addRow(self.MiMa_Label, self.MiMa_LineEdit)
        self.FormLayout.addRow(self.QueRenMiMa_Label, self.QueRenMiMa_LineEdit)
        self.FormLayout.addRow('', self.ZhuCe_Button)

        self.VBoxLayout = QVBoxLayout()
        self.setLayout(self.VBoxLayout)

        self.VBoxLayout.addWidget(self.BiaoTi_Label, Qt.AlignHCenter)

        FormLayout_Widget = QWidget()
        FormLayout_Widget.setLayout(self.FormLayout)

        FormLayout_Widget.setFixedWidth(300)
        FormLayout_Widget.setFixedHeight(250)

        self.ShuiPing_BuJu = QHBoxLayout()
        self.ShuiPing_BuJu.addWidget(FormLayout_Widget, Qt.AlignCenter)

        FormLayout_Widget = QWidget()
        FormLayout_Widget.setLayout(self.ShuiPing_BuJu)

        self.VBoxLayout.addWidget(FormLayout_Widget, Qt.AlignHCenter)

        reg1 = QRegExp("[1-9][0-9]+$")
        YanZhengQi = QRegExpValidator(self)
        YanZhengQi.setRegExp(reg1)
        self.ZhangHao_LineEdit.setValidator(YanZhengQi)

        reg2 = QRegExp("[A-Z][a-z][0-9]+$")
        YanZhengQi = QRegExpValidator(self)
        YanZhengQi.setRegExp(reg2)
        self.MiMa_LineEdit.setValidator(YanZhengQi)
        self.QueRenMiMa_LineEdit.setValidator(YanZhengQi)

        self.ZhuCe_Button.clicked.connect(self.ZhuCe_Button_clicked)
        self.ZhangHao_LineEdit.returnPressed.connect(self.ZhuCe_Button_clicked)
        self.XingMing_LineEdit.returnPressed.connect(self.ZhuCe_Button_clicked)
        self.MiMa_LineEdit.returnPressed.connect(self.ZhuCe_Button_clicked)
        self.QueRenMiMa_LineEdit.returnPressed.connect(self.ZhuCe_Button_clicked)

    def ZhuCe_Button_clicked(self):
        zhanghao = self.ZhangHao_LineEdit.text()
        xingming  = self.XingMing_LineEdit.text()
        mima = self.MiMa_LineEdit.text()
        querenmima = self.QueRenMiMa_LineEdit.text()
        if(zhanghao=='' or xingming=='' or mima=='' or querenmima==''):
            print(QMessageBox.warning(self, "警告", "请输入完整信息！", QMessageBox.Yes, QMessageBox.Yes))
            return
        else:
            ShuJuKu = QSqlDatabase.addDatabase("QSQLITE")
            ShuJuKu.setDatabaseName('./db/Image_database.db')
            ShuJuKu.open()
            query = QSqlQuery()
            sql_1 = "select * from user where zhanghao = '%s'" % zhanghao
            query.exec_(sql_1)

            if (mima != querenmima):
                print(QMessageBox.warning(self, '警告：', '两次密码输入不一致！', QMessageBox.Yes, QMessageBox.Yes))
                return
            elif(mima == querenmima):
                sql_2 = "select * from user where ZhangHao='%s'" % zhanghao
                query.exec_(sql_2)
                if (query.next()):
                    print(QMessageBox.warning(self, '警告：', '该账号已经注册，请重新输入帐号！', QMessageBox.Yes, QMessageBox.Yes))
                    self.ZhangHao_LineEdit.clear()
                    self.XingMing_LineEdit.clear()
                    return
                else:
                    sql_3 = "INSERT INTO User VALUES('%s','%s','%s',0,0,0,0)" % (zhanghao, xingming, mima)
                    query.exec_(sql_3)
                    ShuJuKu.commit()
                    print(QMessageBox.information(self, '提示', '恭喜，您成功注册帐号！', QMessageBox.Yes, QMessageBox.Yes))
                    self.Register_success_signal.emit(zhanghao)
                ShuJuKu.close()
                return

if __name__ == '__main__':
    from sys import argv, exit
    app = QApplication(argv)

    app.setStyleSheet(load_stylesheet_pyqt5())
    window = Register_Widget()
    window.show()
    exit(app.exec_())